package com.jycrm.dao.sms.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;

import org.springframework.orm.hibernate.HibernateCallback;

import com.jycrm.dao.core.AbstractService;
import com.jycrm.dao.sms.ISmsTargetDao;
import com.jycrm.domain.sms.SmsTarget;

public class SmsTargetDaoImpl extends AbstractService implements ISmsTargetDao {

	public void save(SmsTarget target) {
		persistenceManager.create(target);

	}

	public void update(SmsTarget target) {
		persistenceManager.update(target);

	}

	public void delete(SmsTarget target) {
		persistenceManager.delete(target);

	}

	public SmsTarget getSmsTargetByID(Long id) {
		return (SmsTarget) queryManager.getObjectById(SmsTarget.class, id);

	}
	
	public List getSmsTargetByAnalysis(Boolean alysis){
		//List list = queryManager.findByNamedQuery("getSmsTargetByAnalysis", alysis);
		int al = 0;
		if(alysis !=null){
			al = alysis.booleanValue()?1:0;
		}
		final int a =al; 
			
		
		
		List rs = this.queryManager.findPagination(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException {
				StringBuffer queryStr = new StringBuffer(
						"from SmsTarget target inner join fetch target.info where target.analysised =  " + a +	
						" order by target.info.priority ");
				Query query = session.createQuery(queryStr.toString());
				query.setMaxResults(500);
				query.setFirstResult(0);
				return query.list();
			}
			});
		return rs;
		
		
		//return list;
	}

	public void executeAnalysisCustomer(String tempSmsTargetTable) {
		Map map=new HashMap();
		map.put("crm_sms_sendtarget",tempSmsTargetTable);
		this.sqlMapManager.getSqlMapClientTemplate().queryForObject("analysisSmsCustomer",map);
		
	}
	
	public void executeAnalysisSmsContactperson(String tempSmsTargetTable) {
		Map map=new HashMap();
		map.put("crm_sms_sendtarget",tempSmsTargetTable);
		this.sqlMapManager.getSqlMapClientTemplate().queryForObject("analysisSmsContactperson",map);
		
	}
	
	public void executeAnalysisSmsFellowship(String tempSmsTargetTable) {
		Map map=new HashMap();
		map.put("crm_sms_sendtarget",tempSmsTargetTable);
		this.sqlMapManager.getSqlMapClientTemplate().queryForObject("analysisSmsFellowship",map);
		
	}
	
	public void executeAnalysisSmsChannel(String tempSmsTargetTable) {
		Map map=new HashMap();
		map.put("crm_sms_sendtarget",tempSmsTargetTable);
		this.sqlMapManager.getSqlMapClientTemplate().queryForObject("analysisSmsChannel",map);
		
	}
	
	public void createTemSmsSendTargeTable(String tempSmsTargetTable){
		Map map=new HashMap();
		map.put("TemSmsSendTargeTable",tempSmsTargetTable);
		this.sqlMapManager.getSqlMapClientTemplate().queryForObject("createTemSmsSendTargeTable",map);
		
	}
	
	public void dropTemSmsSendTargeTable(String tempSmsTargetTable){
		Map map=new HashMap();
		map.put("TemSmsSendTargeTable",tempSmsTargetTable);
		this.sqlMapManager.getSqlMapClientTemplate().queryForObject("dropTemSmsSendTargeTable",map);
		
	}
	
	
	public void updateCrmSmsSendTargetAnalysised(String tempSmsTargetTable){
		Map map=new HashMap();
		map.put("TemSmsSendTargeTable",tempSmsTargetTable);
		this.sqlMapManager.getSqlMapClientTemplate().queryForObject("update_crm_sms_sendtarget_analysised",null);
	}
	
	public List getSmsTargetUnAnalysised(String tempSmsTargetTable){
		Map map=new HashMap();
		map.put("TemSmsSendTargeTable",tempSmsTargetTable);
		return this.sqlMapManager.queryForList("getSmsTargetUnAnalysised",map);
	}
	
	public void executeAnalysisSmsCustomerGroup(String crm_customers,Long smsId, Long targetId,String sendtaoperator,
			String sendbizoperator, String sendrknreceiver,String sendkeycontactperson, String sendothercontactperson){
		
		
		Map map=new HashMap();
		map.put("smsId",smsId);
		map.put("targetId",targetId);
		
		map.put("sendtaoperator",sendtaoperator);
		map.put("sendbizoperator",sendbizoperator);
		
		map.put("sendrknreceiver",sendrknreceiver);
		map.put("sendkeycontactperson",sendkeycontactperson);
		map.put("sendothercontactperson",sendothercontactperson);
		map.put("crm_customers",crm_customers);
		
		
		this.sqlMapManager.queryForObject("analysisSmsCustomerGroup",map);
	}
	
	
	public void executeAnalysisSmsChannelGroup(String crm_channels,Long smsId, Long targetId,String sendtaoperator,
			String sendbizoperator, String sendrknreceiver,String sendkeycontactperson, String sendothercontactperson){
		
		
		Map map=new HashMap();
		map.put("smsId",smsId);
		map.put("targetId",targetId);
		
		map.put("sendtaoperator",sendtaoperator);
		map.put("sendbizoperator",sendbizoperator);
		
		map.put("sendrknreceiver",sendrknreceiver);
		map.put("sendkeycontactperson",sendkeycontactperson);
		map.put("sendothercontactperson",sendothercontactperson);
		map.put("crm_channels",crm_channels);
		
		
		 this.sqlMapManager.queryForObject("analysisSmsChannelGroup",map);
	}
	
	
	public void executeAnalysisSmsContactpersonGroup(String crm_contactperson,Long smsId, Long targetId,String sendtaoperator,
			String sendbizoperator, String sendrknreceiver,String sendkeycontactperson, String sendothercontactperson){
		
		
		Map map=new HashMap();
		map.put("smsId",smsId);
		map.put("targetId",targetId);
		
		map.put("sendtaoperator",sendtaoperator);
		map.put("sendbizoperator",sendbizoperator);
		
		map.put("sendrknreceiver",sendrknreceiver);
		map.put("sendkeycontactperson",sendkeycontactperson);
		map.put("sendothercontactperson",sendothercontactperson);
		map.put("crm_contactperson",crm_contactperson);
		
		
		 this.sqlMapManager.queryForObject("analysisSmsContactpersonGroup",map);
	}

	
	public void analysisSmsMobile(){
	this.sqlMapManager.insert("analysisSmsMobile",null);	
	}
	
	public void deleteSmsSendTargetByCrmTempSmsDeleteTable(){
		this.sqlMapManager.getSqlMapClientTemplate().delete("deleteSmsSendTargetByTemp",null);
	}

}
